package com.zhijian.wharf.aop;

import com.zhijian.wharf.util.AnnotationValidationUtils;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

import java.util.Arrays;
import java.util.Objects;

@Aspect
@Component
public class ValidateAop {

    @Pointcut("@annotation(com.zhijian.response.annotation.Response)")
    public void toValidPointCut() {
        // Do nothing because of pointcut
    }

    @Before("toValidPointCut()")
    public void before(JoinPoint joinPoint) {
        Object[] args = joinPoint.getArgs();
        if (args.length > 0) {
            Arrays.stream(args).forEach(a -> {
                if (!Objects.isNull(a)) {
                    AnnotationValidationUtils.validate(a);
                }
            });

        }
    }
}
